package co.edu.unal.geditor.controller.action.undoable;

import javax.swing.undo.AbstractUndoableEdit;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;

import co.edu.unal.geditor.controller.App;
import co.edu.unal.geditor.model.Drawing;
import co.edu.unal.geditor.model.GraphElement;

public class UndoableCreate extends AbstractUndoableEdit
{
	private static final long serialVersionUID = 1L;

	public UndoableCreate( GraphElement ge, int idx )
	{
		assert ge != null;
		assert idx >= 0;
		
		m_ge = ge;
		m_idx = idx;
	}

	public void undo() throws CannotUndoException
	{
		Drawing drawing = App.getInstance().getDrawing();

		drawing.removeElementAt( m_idx );
	}

	public void redo() throws CannotRedoException
	{
		Drawing drawing = App.getInstance().getDrawing();
		
		drawing.insertElementAt( m_ge, m_idx );
	}

	public boolean canUndo()
	{
		return (m_ge != null);
	}

	public boolean canRedo()
	{
		return (m_ge != null);
	}

	public String getPresentationName()
	{
		return "Create Element";
	}

	private GraphElement m_ge;
	private int m_idx;
}
